﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseUserTest.DB
{
    public class DBRoute
    {
        static DatabaseUserTest.DatabaseContext db = new DatabaseUserTest.DatabaseContext();

        // Route is found based on the FromDestination and the ToDestination
        public Route FindRoute(Destination from, Destination to)
        {   
            
            foreach (Route item in db.Routes)
            {
                if (item.FromDestination == from.Name && item.ToDestination == to.Name)
                {
                    return item;
                }
            }
            return null;
        }

        public Route FindRoute(int id)
        {
            return db.Routes.Find(id);
        }

        public List<Route> FindAllRoutes()
        {
            List<Route> routes = new List<Route>();
            var query = from r in db.Routes select r;
            

            foreach (Route item in query)
            {
                routes.Add(item);
            }
            return routes;
        }

        // Route is created in the UI/Controller and added to DB here.
        public void AddRoute(Route route)
        {
            db.Routes.Add(route);
            db.SaveChanges();
        }
    }
}
